/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myservlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.controldb;
import db.linkdb;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.http.Cookie;

/**
 *
 * @author 阿椋世界第二可爱
 */
public class Getpost extends HttpServlet {
      private static final long serialVersionUID = 1L;

    public Getpost() {
        super();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String radiobutton1 = request.getParameter("radiobutton"); //获取客户端是否记住密码状态

         //生成cookie
         if(radiobutton1!= null){ //如果记住密码不为空即选择记住密码
         Cookie nameCookie = new Cookie("username", URLEncoder.encode(username, "utf-8")); //创建nameCookie，名字是username
         Cookie passwordCookie=new Cookie("password",URLEncoder.encode(password, "utf-8")); //创建passwordCookie，名字是password 
         passwordCookie.setMaxAge(30*24*60*60); //设置passwordCookie的过期时间
         nameCookie.setMaxAge(30*24*60*60); //设置nameCookie的过期时间
         response.addCookie(nameCookie); //添加nameCookie到服务器端
         response.addCookie(passwordCookie);//添加passwordCookie到服务器端
                              }
         PrintWriter out = response.getWriter(); //response顾名思义就是服务器度对浏览器的响应，PrintWriter它的实例就是问前台的JSP页面输出结果

         
        controldb temp=new controldb();
        temp.dealcontroldb();
        boolean x=temp.verify(username, password);
        if(x==false) 
        {
                response.sendRedirect("QAQSorry.jsp");	
        }
        else
        {       request.getSession().setAttribute("userSession", username);
        	response.sendRedirect("welcome.jsp");
        }
        temp.close();
	}

}

    